// pages/userConsole/userConsole.js
import Toast from '../../dist/toast/toast';
const db = wx.cloud.database()
Page({

  data: {
    openid: '',
    userInfodata: {},
    show: false,
    shows: false,
    fileList: [

    ],
    folder: 'opinion',
    imgurllistL: [],
    opinionmessage: '',
    contactmessage: '',
    loading: false

  },

  onLoad: function (options) {
    let that = this
    Toast(`欢迎 ${wx.getStorageSync('name')} 登录系统!`);
    wx.getUserInfo({
      success(res) {
        console.log(res)
        that.setData({
          userInfodata: res.userInfo
        })
      }
    })
  },
  opinionmessages(e) {
    console.log(e)
    this.setData({
      opinionmessage: e.detail
    })
  },
  contactmessages(e) {
    console.log(e)
    this.setData({
      contactmessage: e.detail
    })
  },
  onShow() {
    // this.setData({
    //   show: true
    // })
  },
  wuquan() {
    Toast(`暂无权限哦！`);
  },
  toindex() {
    wx.redirectTo({
      url: '../signin/signin',
    })
  },
  about() {
    this.setData({
      show: true
    })
  },
  opinion() {
    this.setData({
      shows: true
    })
  },
  getUserInfo: function (e) {
    let that = this;
    // console.log(e)
    // 获取用户信息
    wx.getSetting({
      success(res) {
        // console.log("res", res)
        if (res.authSetting['scope.userInfo']) {
          console.log("已授权=====")
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success(res) {
              console.log("获取用户信息成功", res)
              that.setData({
                userInfodata: res.userInfo
              })
            },
            fail(res) {
              console.log("获取用户信息失败", res)
            }
          })
        } else {
          console.log("未授权=====")
        }
      }
    })
  },
  afterRead(event) {
    const file = event.detail.file;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    console.log(file)
    this.data.fileList.unshift(file)
    var files = this.data.fileList
    var path = this.data.folder + '/' + Math.random().toString();
    this.setData({
      fileList: files,
      path
    })

  },
  uploadToCloud() {

    wx.cloud.init();
    const { fileList } = this.data;
    if (!fileList.length) {
      wx.showToast({ title: '请选择图片', icon: 'none' });
    } else {
      const uploadTasks = fileList.map((file, index) => this.uploadFilePromise(`my-photo${index}.png`, file));
      Promise.all(uploadTasks)
        .then(data => {
          wx.showToast({ title: '上传成功', icon: 'none' });

          const newFileList = data.map(item => ({ url: item.fileID }));

          this.setData({ cloudPath: data, fileList: newFileList });
          console.log(this.data.fileList)

        })
        .catch(e => {
          wx.showToast({ title: '上传失败', icon: 'none' });
          console.log(e);
        });
      return Promise.resolve(this.data.fileList)
    }
  },
  uploadFilePromise(fileName, chooseResult) {
    return wx.cloud.uploadFile({
      cloudPath: this.data.path + fileName,
      filePath: chooseResult.path
    });
  },
  deleteimg(e) {
    this.setData({
      fileList: []
    })
  },
  commitdata() {
    let that = this



    if (!this.data.opinionmessage) {
      wx.showToast({ title: '请填写意见!', icon: 'none' });
    } else {
      this.setData({
        loading: true
      })
      that.uploadToCloud()
      setTimeout(() => {
        let postdata = {
          opinion: that.data.opinionmessage,
          contact: that.data.contactmessage,
          imgurl: that.data.fileList
        }
        db.collection('opinion').add({
          data: postdata
        }).then(res => {
          Toast.success('提交成功！');
          that.setData({
            opinionmessage: '',
            contactmessage: '',
            fileList: [],
            loading: false,
            shows:false
          })
        })
      }, 1500)
    }
  }
})